﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Text;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Drawing;

namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class hospitalAmountSubsidyStatistics5 : BasePage
    {
        DataTable dt;
        DataTable dt2;
        int state = 1;//显示的图片状态(0:线状,1:柱状,2:饼状)
        int sort = 2; //要显示的数据类型(0:处方单基药金额与补贴金额,1:使用基药比率  2:排名统计)
        int a =1;
        protected void Page_Load(object sender, EventArgs e)
        {
            txtProName.Attributes.Add("readonly", "readonly");
            if (!IsPostBack)
            {
                if (Request["state"] != null)
                {
                    state = int.Parse(Request["state"]);
                }

                if (Request["sort"] != null)
                {
                    sort = int.Parse(Request["sort"]);
                }
                BindArea();
                dataBind(sort, state);
                HidSort.Value = sort.ToString();
                HidState.Value = state.ToString();
            }
            else
            {
                sort = int.Parse(HidSort.Value);
                state = int.Parse(HidState.Value);
            }


        }

        /// <summary>    
        /// 查询数据 type:0:医院已确认补助,1:已补助
        /// </summary>
        private void dataBind(int type, int t)
        {
            HZ.Supervise.IBLL.IBLL_Prescription iPrescription = HZ.Supervise.BLLFactory.IBl_Prescription();
            StringBuilder sql = new StringBuilder();
            StringBuilder sql2 = new StringBuilder();
            string sqlgroup = "";
            //if (type == 0)
            //{
            //    //if (!string.IsNullOrEmpty(this.txtHosName.Text.Trim()))
            //    //{
            //    //    sql.Append(" and SHospitalName like '%" + this.txtHosName.Text + "%' ");
            //    //}
            //    //if (ddlCity.SelectedIndex != 0)
            //    //{
            //    //    sql.Append(" and Sareaid like '%" + ddlCity.SelectedValue.ToString().TrimEnd('0') + "%'");
            //    //}
            //    if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
            //    {
            //        sql.Append(" and SubsidyStartTime >= '" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
            //    }
            //    if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            //    {
            //        sql.Append(" and SubsidyEndTime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
            //    }
            //    sql.Append(" and State = 3");
            //    sqlgroup = " dbo.fun_GetYearMonth(SconfirmationTime)";

            //    HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            //    StringBuilder sqlwhere = new StringBuilder();
            //    sqlwhere.Append("select MAX(Sareaname) as Sareaname ,MAX(sareaid) as sareaid ,MAX(sHospitalId) as sHospitalId,MAX(SHospitalName) as SHospitalName,");
            //    sqlwhere.Append("dbo.fun_GetYearMonth(SconfirmationTime) as Time ,0 as temp , SUM(SsumAmount) AS SsumAmount,SUM(SubsidyAmount) as SubsidyAmount ,MAX(Sadduserid) as Sadduserid  from TB_Subsidy where 1=1 " + sql + " group by " + sqlgroup);

            //    dt = ie.FixAllMonth(iPrescription.GetSubsidyStatistics(sqlwhere.ToString()), 1, DateTime.Now.Year, "Time");
            //}
            //else if (type == 1)
            //{
            //    StringBuilder sqlwhere = new StringBuilder();
            //    //if (!string.IsNullOrEmpty(this.txtHosName.Text.Trim()))
            //    //{
            //    //    sqlwhere.Append(" and PHospitalName like '%" + this.txtHosName.Text + "%' ");
            //    //}
            //    //if (ddlCity.SelectedIndex != 0)
            //    //{
            //    //    sqlwhere.Append(" and PAreaId like '%" + ddlCity.SelectedValue.ToString().TrimEnd('0') + "%'");
            //    //}
            //    if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
            //    {
            //        sqlwhere.Append(" and Paddtime >= '" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
            //    }
            //    if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
            //    {
            //        sqlwhere.Append(" and Paddtime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
            //    }
            //    sql.Append("select SUM(Pamount) as Pamount, sum(Pcount) as Pcount ,PsortSource from TB_PrescriptionDetail where PrescriptionID in( select PrescriptionId from TB_Prescription where 1=1 " + sqlwhere.ToString() + ") group by PsortSource");
            //    dt = iPrescription.GetSubsidyStatistics(sql.ToString());
            //}
            //else
            //{
                StringBuilder sqlwhere = new StringBuilder();
                //if (!string.IsNullOrEmpty(this.HidHosId.Value))
                //{
                //    sqlwhere.Append(" and PHospitalID = '" + this.HidHosId.Value + "' ");
                //}
                //if (ddlCity.SelectedIndex != 0)
                //{
                //    sqlwhere.Append(" and PAreaId like '%" + ddlCity.SelectedValue.ToString().TrimEnd('0') + "%'");
                //}
                if (!string.IsNullOrEmpty(HidProId.Value.Trim()))
                {
                    //获得选取的药品ID
                    string[] pids =HidProId.Value.Split(',');
                    StringBuilder strPid = new StringBuilder();
                    //整合PID
                    foreach (var item in pids)
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            strPid.Append("'" + item + "',");
                        }
                    }
                    sqlwhere.Append(" and ProcureCatalogID in ("+strPid.Remove(strPid.Length-1,1).ToString()+")");
                }
                if (!string.IsNullOrEmpty(txtStratTime.Text.Trim()))
                {
                    sqlwhere.Append(" and PSubsidyTime >= '" + (new HZ.Common.ConvertHelper(txtStratTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
                }
                if (!string.IsNullOrEmpty(txtEndTime.Text.Trim()))
                {
                    sqlwhere.Append(" and PSubsidyTime <='" + (new HZ.Common.ConvertHelper(txtEndTime.Text.Trim()).ToDateTime).Value.ToString() + "'");
                }
                //if (a == 0)
                //{
                //    sql.Append("select top 10 SUM(Pamount) as Pamount, sum(Pcount) as Pcount ,ProcureCatalogID,max(ProductName) as ProductName  from TB_PrescriptionDetail where PrescriptionID in( select PrescriptionId from TB_Prescription where 1=1 " + sqlwhere.ToString() + ") group by ProcureCatalogID order by Pcount desc");
                //}
                //else
                //{
                //    sql.Append("select top 10 SUM(Pamount) as Pamount, sum(Pcount) as Pcount ,ProcureCatalogID,max(ProductName) as ProductName  from TB_PrescriptionDetail where PrescriptionID in( select PrescriptionId from TB_Prescription where 1=1 " + sqlwhere.ToString() + ") group by ProcureCatalogID order by Pamount desc");
                //}
                sql.Append(@"SELECT TOP 10  PAreaname 地区名称,SUM(Pamount) 处方单总金额  FROM view_Prescription WHERE POrderStatus<>0 " + sqlwhere + " GROUP BY PAreaname having SUM(Pamount) >0 ORDER BY SUM(Pamount) desc");       

                dt = iPrescription.GetSubsidyStatistics(sql.ToString());
                sql2.Append(@"SELECT TOP 10  PAreaname 地区名称,sum(PSubsidiesAmount) 补助总金额 FROM view_Prescription WHERE POrderStatus<>0 " + sqlwhere + " GROUP BY PAreaname having SUM(Pamount) >0 ORDER BY SUM(PSubsidiesAmount) desc"); 
                dt2 = iPrescription.GetSubsidyStatistics(sql2.ToString());
            //}

            BindChart(t);
        }
        /// <summary>
        /// 绑定地区
        /// </summary>
        public void BindArea()
        {
            //IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
            //IList<Area> lstArea = new List<Area>();
            //string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
            ////判断用户所属地区，如果用户是省级的 则可以列出全部区域
            ////if (userAreaId == CurrentAccount.GetCurrentDeployArea().AreaID)
            ////{
            //int i;
            //lstArea = ia.GetSonAreas(userAreaId, 1, 0, out i);
            //foreach (Area area in lstArea)
            //{
            //    ListItem ddlitem = new ListItem();
            //    ddlitem.Text = area.AreaName;
            //    ddlitem.Value = area.AreaID;
            //    ddlCity.Items.Add(ddlitem);
            //}

            //ListItem b = new ListItem();
            //b.Text = "全部";
            //b.Value = "";
            //ddlCity.Items.Insert(0, b);


        }
        /// <summary>
        /// 图表绑定 (初始)
        /// </summary>
        private void BindChart(int type)
        {

            //空为初始页
            if (dt.Rows.Count > 0)
            {
                //string AreaName = "";//月份
                //int count = 2;//次数
                //string sort = "0";//属性 0:基药 1:非基药
                SeriesChartType t;
                switch (type)
                {
                    case 0:
                        t = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Spline", true);
                        break;
                    case 1:
                        t = SeriesChartType.Column;
                        break;
                    case 2:
                        t = SeriesChartType.Pie;
                        break;
                    default:
                        t = SeriesChartType.Column;
                        break;
                }

                Series series1;
                Series series2;
                //if (sort == 0)
                //{
                //    #region 图

                //    //***************** 已补贴金额 单位：万元*****************/
                //    series1 = new Series("已补贴金额 单位：万元");
                //    series1.ChartType = t;
                //    series1.BorderWidth = 1;
                //    series1.ShadowOffset = 1;

                //    //***************** 处方单金额*******************/
                //    series2 = new Series("基药总金额 单位：万元");
                //    series2.ChartType = t; //(SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                //    series2.BorderWidth = 1;
                //    series2.ShadowOffset = 1;

                //    #endregion
                //    #region 绑定值
                //    for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                //    {

                //        if (dt.Rows[i]["temp"].ToString().Equals("0"))
                //        {
                //            series2.Points.AddXY(dt.Rows[i]["Time"].ToString().ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["Samount"].ToString()) / 10000).ToString());
                //            series1.Points.AddXY(dt.Rows[i]["Time"].ToString().ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["SubsidyAmount"].ToString()) / 10000).ToString());
                //        }
                //    }
                //    #endregion
                //}
                //else if (sort == 1)
                //{
                //    #region 图

                //    //***************** 已补贴金额 单位：万元*****************/
                //    series1 = new Series("总金额 单位：万元");
                //    series1.ChartType = SeriesChartType.Pie;
                //    series1.BorderWidth = 1;
                //    series1.ShadowOffset = 1;

                //    //***************** 处方单金额*******************/
                //    series2 = new Series("总数量");
                //    //series2.ChartType = SeriesChartType.Pie; ; //(SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                //    //series2.BorderWidth = 1;
                //    //series2.ShadowOffset = 1;

                //    #endregion

                //    series1.Points.AddXY("基本药品", (double.Parse(dt.Rows[0]["Pamount"].ToString()) / 10000).ToString());
                //    series1.Points.AddXY("非基本药品", (double.Parse(dt.Rows[1]["Pamount"].ToString()) / 10000).ToString());
                //}
                //else
                //{
                #region 图

                //***************** 已补贴金额 单位：万元*****************/
                series1 = new Series("金额(万元)");
                series1.ChartType = t;
                series1.BorderWidth = 1;
                series1.ShadowOffset = 1;

                //***************** 处方单金额*******************/
                series2 = new Series("金额(万元)");
                series2.ChartType = t; //(SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series2.BorderWidth = 1;
                series2.ShadowOffset = 1;
                if (type==2)
                {   series1["PieDrawingStyle"] = "Concave";
                    series2["PieDrawingStyle"] = "Concave";
                    series1.Label = "#PERCENT{P1}";
                    series2.Label = "#PERCENT{P1}";
                }
                else if (type==1)
                {
                    series1["PointWidth"] = "0.3";
                    series2["PointWidth"] = "0.3";
                    series1["DrawingStyle"] = "Emboss";
                    series2["DrawingStyle"] = "Emboss";

                }
                for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                {


                    series1.Points.AddXY(i + 1, (double.Parse(dt.Rows[i]["处方单总金额"].ToString()) / 10000).ToString("0.0000"));
                    series1.Points[i].ToolTip = dt.Rows[i]["地区名称"].ToString();
                    series1.Points[i].Label = string.Format("{0}:{1}", dt.Rows[i]["地区名称"].ToString(), (double.Parse(dt.Rows[i]["处方单总金额"].ToString()) / 10000).ToString("0.0000"));

                    series2.Points.AddXY(i + 1, (double.Parse(dt2.Rows[i]["补助总金额"].ToString())).ToString());
                    series2.Points[i].ToolTip = dt2.Rows[i]["地区名称"].ToString();
                    series2.Points[i].Label = string.Format("{0}:{1}", dt2.Rows[i]["地区名称"].ToString(), (double.Parse(dt2.Rows[i]["补助总金额"].ToString()) / 10000).ToString("0.0000"));


                    if (type==2)
                    {
                        series1.Points[i].LegendText = dt.Rows[i]["地区名称"].ToString();
                        series2.Points[i].LegendText = dt2.Rows[i]["地区名称"].ToString();
                    }
                }
                #endregion
                //}



                #region 数据绑定到图
                //左图
                series1.IsValueShownAsLabel = true;
                hospitalAmountSubsidy.Series.Add(series1);
                series2.IsValueShownAsLabel = true;
                hospitalAmountSubsidy0.Series.Add(series2);
                #endregion

                #region 两张图片属性
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.TextOrientation = TextOrientation.Horizontal;
                //hospitalAmountSubsidy.Series["hospitalAmountSubsidy"]["hospitalAmountSubsidy"] = PointLabelsList.SelectedItem.Text;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.LineWidth = 1;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy.Series["金额(万元)"].IsValueShownAsLabel = true;
                hospitalAmountSubsidy.Titles.Add("地区补贴总金额排名图");
                hospitalAmountSubsidy.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
                hospitalAmountSubsidy.Titles[0].Position.X = 30;
                hospitalAmountSubsidy.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisY.Title = "金额(万元)";
                hospitalAmountSubsidy.ChartAreas["hospitalAmountSubsidy"].AxisX.Title = "名次";

                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.IsMarginVisible = true;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].Area3DStyle.Enable3D = false;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.TextOrientation = TextOrientation.Horizontal;
                //hospitalAmountSubsidy.Series["hospitalAmountSubsidy"]["hospitalAmountSubsidy"] = PointLabelsList.SelectedItem.Text;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.LineWidth = 1;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.IntervalOffset = 1;
                hospitalAmountSubsidy0.Series["金额(万元)"].IsValueShownAsLabel = true;
                hospitalAmountSubsidy0.Titles.Add("地区处方单总金额排名图");
                hospitalAmountSubsidy0.Titles[0].Font = new Font("宋体", 16, FontStyle.Bold);
                hospitalAmountSubsidy0.Titles[0].Position.X = 30;
                hospitalAmountSubsidy0.Titles[0].Position.Y = 1;
                hospitalAmountSubsidy0.Titles[0].Position.Width = 50;
                hospitalAmountSubsidy0.Titles[0].Position.Height = 8;
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisY.Title = "金额(万元)";
                hospitalAmountSubsidy0.ChartAreas["hospitalAmountSubsidy0"].AxisX.Title = "名次";



                hospitalAmountSubsidy.Series["金额(万元)"].MarkerStyle = MarkerStyle.Square;
                hospitalAmountSubsidy0.Series["金额(万元)"].MarkerStyle = MarkerStyle.Square;
                //if (series2.Name == "数量")
                //{
                //    hospitalAmountSubsidy.Series["金额"].YAxisType = AxisType.Secondary;
                //    hospitalAmountSubsidy0.Series["数量"].YAxisType = AxisType.Primary;
                //}
                #endregion

            }
            else
            {
                //this.ChartInStorage.Visible = false;
                this.hospitalAmountSubsidy.Visible = false;
                //lblnull.Visible = true;
            }

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            dataBind(sort, state);
        }

        protected void HzButton3_Click(object sender, EventArgs e)
        {
            HidState.Value = "2";
            state = 2;
            dataBind(sort, state);
        }

        protected void HzButton2_Click(object sender, EventArgs e)
        {

            HidState.Value = "0";
            state = 0;
            dataBind(sort, state);
        }

        protected void HzButton1_Click(object sender, EventArgs e)
        {
            HidState.Value = "1";
            state = 1;
            dataBind(sort, state);
        }

      

        protected void HzButton6_Click(object sender, EventArgs e)
        {
            HidSort.Value = "2";
            sort = 2;

            dataBind(sort, state);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            a = 1;
            dataBind(sort, state);
            this.hospitalAmountSubsidy.Visible = true;
            this.hospitalAmountSubsidy0.Visible = false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            a = 0;
            dataBind(sort, state);
            this.hospitalAmountSubsidy.Visible = false;
            this.hospitalAmountSubsidy0.Visible = true;
        }
    }
}
